import configparser
from src.tool import throw
import json
import src.constant as constant
from src.net import Server
from database.dbop import Database
import os
import src.global_config as gl
import src.tool as tool
from src.log import Log

# 配置文件地址
filename = './config.ini'

# 主函数
if __name__ == '__main__':

    # 读取程序配置
    config = configparser.ConfigParser()
    config.read(filename)
    port = config['tcp']['port']

    # 设置全局变量
    dbpath = os.path.dirname(os.path.realpath(__file__))
    gl.set_value('logpath', dbpath)
    # dbpath = os.path.join(dbpath, config['db']['filepath'])
    # gl.set_value('dbpath', dbpath)
    # 一定要在设置完全局变量再实例化
    old = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'old'))
    current = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'current'))
    ai = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'ai'))
    Tom = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'Tom'))

    # 获取数据集
    model = {
        "old": old,
        'current': current,
        'ai': ai,
        "Tom": Tom
    }
    gl.set_value('model' ,model)
    log = Log()
    # 没有端口就抛出错误
    if port is None:
        log.tocrit("缺少端口号")
        throw(format(constant.LACK_OF_PARAMS, 'port'))

    net = Server()
    net.init()
    net.run(port)
